/**
 * 核心技术支持
 * IT渠道网 小程序商城
 * 13873174181 微信同号
 */
import {Category} from 'category-model.js';
import {Cart} from '../cart/cart-model.js';
var category = new Category();
var cart = new Cart();

Page({
  data: {
    transClassArr: ['tanslate0','tanslate1','tanslate2','tanslate3','tanslate4','tanslate5','tanslate6',
                    'tanslate7','tanslate8','tanslate9','tanslate10','tanslate11','tanslate12','tanslate13'],
    currentMenuIndex: 0,
    loadingHidden: false
    //categoryTypeArr: null,
    //categoryInfo0: null
  },
  onLoad: function() {
    this._loadData();
  },
  /**
   * tabbar
   */
  onShow: function() {
    var num = cart.getCartTotalCounts().counts2;
    if (num > 0) {
      wx.setTabBarBadge({
        index: 2,
        text: num.toString()
      })
    } else {
      wx.removeTabBarBadge({
        index: 2
      })
    }
  },
  /**
   * 加载数据
   */
  _loadData: function(callback) {
    var that = this;
    // 获取所有
    category.getCategoryType((categoryData)=>{
      // 截取
      categoryData.forEach((item) => {
        item.name = item.name.substring(0, 5).replace('/','');
      })
      that.setData({
        categoryTypeArr: categoryData
      });
      // 获得单独分类商品
      that.getProductsByCategory(categoryData[0].id, (data)=>{ // 默认首个分类商品 切换
        var dataObj = {
          procucts: data,
          topImgUrl: categoryData[0].image,
          title: categoryData[0].name
        };
        that.setData({
          loadingHidden: true,
          categoryInfo0: dataObj // 返回{topImgUrl,title,procucts[{},]}
        });
        callback && callback();
      });
    })
  },
  getProductsByCategory: function(id, callback) {
    category.getProductsByCategory(id, (data) => {
      callback && callback(data);
    });
  },
  /**
   * 切换
   */
  changeCategory: function(event) {
    var index = category.getDataSet(event,'index'),
        id = category.getDataSet(event,'id');
    this.setData({
      currentMenuIndex: index
    });
    // 数据请求执行
    if (!this.isLoadedData(index)) {
      var that = this;
      // 获得单独分类商品
      this.getProductsByCategory(id, (data)=> {
        that.setData(that.getDataObjForBind(index,data)); // 新类目切换数据,封装处理
      });
    }
  },
  isLoadedData: function(index) {
    if (this.data['categoryInfo'+index]) { // 默认categoryInfo0
      return true;
    }
    return false;
  },
  getDataObjForBind: function(index, data) {
    var obj = {},
        arr = [0,1,2,3,4,5,6,7,8,9,10,11,12,13],
        baseData = this.data.categoryTypeArr[index]; // 获取点击的分类
    for (var item in arr) {
      if (item == arr[index]) {
        obj['categoryInfo'+item] = {   // 如:{categoryInfo1:{},} 此方法内封装除0分类,其他所有分类都封装到obj,一起设置到data
          procucts:data,
          topImgUrl:baseData.image,
          title:baseData.name
        };
        return obj;
      }
    }
  },
  /**
   * 跳转详情
   */
  onProductsItemTap: function(event) {
    var id = category.getDataSet(event, 'id');
    wx.navigateTo({
      url: '../product/product?id=' + id
    })
  },
  /**
   * 下拉刷新
   */
  onPullDownRefresh: function() {
    this._loadData(()=>{
      wx.stopPullDownRefresh()
    });
  },
  /**
   * 分享效果
   */
  onShareAppMessage: function() {
    return {
      title: 'IT渠道网-商城',
      path: 'pages/category/category'
    }
  }
})